<template>
  <view class="login-container">
    <view class="logo-container">
      <image class="logo" src="/static/logo.png"></image>
    </view>
    
    <uni-forms ref="form" :modelValue="formData">
      <uni-forms-item name="phone">
        <uni-easyinput
          v-model="formData.phone"
          type="number"
          placeholder="请输入手机号"
          :maxlength="11"
        >
          <template v-slot:prefix>
            <uni-icons type="phone" size="20" color="#666"></uni-icons>
          </template>
        </uni-easyinput>
      </uni-forms-item>
      
      <uni-forms-item name="password">
        <uni-easyinput
          v-model="formData.password"
          type="password"
          placeholder="请输入密码"
        >
          <template v-slot:prefix>
            <uni-icons type="locked" size="20" color="#666"></uni-icons>
          </template>
        </uni-easyinput>
      </uni-forms-item>
    </uni-forms>

    <button 
      class="login-btn" 
      :class="{'loading': loading}"
      @click="handleLogin"
      :disabled="loading"
    >
      <uni-icons v-if="loading" type="spinner-cycle" size="20" color="#fff"></uni-icons>
      <text>{{ loading ? '登录中...' : '登录' }}</text>
    </button>

    <!-- 添加注册入口 -->
    <view class="register-link" @click="goToRegister">
      <text>还没有账号？</text>
      <text class="link-text">立即注册</text>
    </view>
    </view>
</template>

<script>
import auth from '@/utils/auth.js';

export default {
  data() {
    return {
      formData: {
        phone: '',
        password: ''
      },
      loading: false
    }
  },
  methods: {
    async handleLogin() {
      if (!this.formData.phone || !this.formData.password) {
        uni.showToast({
          title: '请输入手机号和密码',
          icon: 'none'
        });
        return;
      }

      this.loading = true;
      try {
        const userInfo = await auth.login(this.formData.phone, this.formData.password);
        uni.setStorageSync('userInfo', JSON.stringify(userInfo));
        
        // 先显示 Toast，并等待它完成
        await new Promise(resolve => {
          uni.showToast({ 
            title: '登录成功',
            icon: 'success',
            duration: 1500,
            success: resolve
          });
        });
        
        // 延迟跳转，确保 Toast 显示完成
        setTimeout(() => {
          uni.switchTab({
            url: '/pages/index/index',
            fail: (err) => {
              console.error('跳转失败:', err);
              // 如果 switchTab 失败，尝试使用 reLaunch
              uni.reLaunch({
                url: '/pages/index/index'
              });
            }
          });
        }, 1500);
        
      } catch (e) {
        uni.showToast({
          title: e.message || '登录失败',
          icon: 'error'
        });
      } finally {
        this.loading = false;
      }
    },
    // 添加跳转方法
    goToRegister() {
      uni.navigateTo({
        url: '/pages/login/register'
      })
    }
  }
}
</script>

<style>
.login-container {
  padding: 40rpx;
  min-height: 100vh;
  background-color: #fff;
}

.logo-container {
  display: flex;
  justify-content: center;
  margin: 80rpx 0;
}

.logo {
  width: 280rpx;
  height: 280rpx;
}

.login-btn {
  width: 100%;
  height: 88rpx;
  background: #f01a82;
  color: #fff;
  border-radius: 12rpx;
  font-size: 32rpx;
  margin-top: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10rpx;
}

.login-btn.loading {
  opacity: 0.7;
  background: #66b1ff;
}

.login-btn:active {
  opacity: 0.9;
}

/* uni-forms 样式调整 */
:deep(.uni-forms-item) {
  margin-bottom: 30rpx;
}

:deep(.uni-easyinput__content) {
  background-color: #f5f5f5;
  border-radius: 12rpx;
  height: 88rpx;
}

/* 添加注册链接样式 */
.register-link {
  margin-top: 30rpx;
  text-align: center;
  font-size: 28rpx;
  color: #666;
}

.link-text {
  color: #007AFF;
  margin-left: 10rpx;
}
</style>